<template>
  <div>
    <el-dialog
      title="设置置顶"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :lock-scroll="false"
      width="600px"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-row :gutter="20">
            <el-form-item label="置顶序号" prop="orderBy">
              <el-input-number
                v-model="form.orderBy"
                :min="1"
                :max="1000"
                size="medium"
                label="置顶序号"
              ></el-input-number>
              <p class="txt-explain">注：序号越大越靠前</p>
            </el-form-item>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        id: '',
        orderBy: '0',
      },
      rules: {
        orderBy: [
          { required: true, message: '请设置置顶序号', trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {
    this.$bus.$on('setBbsTop', this.init)
  },
  beforeDestroy() {
    this.$bus.$off('setBbsTop')
  },
  methods: {
    init(row) {
      this.form.id = row.id
      this.form.orderBy = 10
      this.dialogVisible = true
    },
    submitForm(form) {
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          await this.$store.dispatch('updateBbsTopic', this.form)
          this.done()
        }
      })
    },
    done() {
      this.dialogVisible = false
      this.$bus.$emit('refreshBbsTopicList')
      this.$message({
        message: '操作成功',
        type: 'success',
      })
    },
  },
}
</script>
